(function(win){
	//图片上传
	/*
		使用方法
		let a=new FoxUpload({
				url:'http://tp5.cc/index/index/imguplaod',//图片上传路径
				uploadimgID:'uploadimg',//默认图id
				uploadButtonID:'uploadimg',//触发上传id
				lodingpath:'loding.gif',//上传中的加载替换图(非必填)
				success:function (data){//上传结果
					data=JSON.parse(data);
                	console.log('返回结果2',data);
					a.setimgsrc(data.data);//设置图片
					console.log(a.getimgpath());//获取图片
					
            	}
		});
	*/
	
	class upload{
		config={
			url:'11',//默认上传地址
			uploadimgID:'',//图片id
			uploadButtonID:'',//触发上传按钮id
			inputfileID:'inputfileID',
			lodingpath:'',
			success:function (data){
                console.log('返回结果',data)
            }
		}
		constructor(selfConfig){
			let self=this;
			self.config=Object.assign({}, self.config, selfConfig);
			//设置按钮
			self.udload_img_el=document.getElementById(self.config.uploadimgID); 
			self.udload_btn_el=document.getElementById(self.config.uploadButtonID);
			self.inputfile_el=document.createElement('input');
            self.inputfile_el.setAttribute('type','file');
            self.inputfile_el.setAttribute('id',self.config.inputfileID);
			console.log(this.config);
			self.udload_btn_el.onclick=function(){
                self.inputfile_el.click();
            }
			//上传
			self.inputfile_el.onchange=function (files){
				if(self.config.lodingpath){
					self.setimgsrc(self.config.lodingpath);
				}
				
                //上传
                //console.log(file);
                //console.log(that.config.url);
                let request=new XMLHttpRequest();
                request.open('POST',self.config.url);
                let form=new FormData();
                form.append('file',self.inputfile_el.files[0]);
                request.send(form);
                request.onreadystatechange=function () {
                    if(request.readyState=='4' && request.status==200){
						//self.setimgsrc(self.config.lodingpath);
                        self.config.success(request.responseText);
                        //json转对象返回
                        //that.config.success(JSON.parse(request.responseText));
                    }
                }
			}

		}
		//设置上传图
		setimgsrc(path){
            this.udload_img_el.setAttribute('src',path);
        }
		//获取上传图片的路径
		getimgpath(){
			return this.udload_img_el.getAttribute('src');
		}

	}
	
	win.FoxUpload=upload;
})(window);